﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web.Mvc;
using System.Text;
using System.EnterpriseServices;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model.ParamModel;
using YiYiTong.Rocket.EFDBFirst;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class BusinessMsgController : BaseController
    {

        [Description("删除消息")]
        [NoVerify]
        public ActionResult delmsg(string ids)
        {
            var userinfo = new CurrentUser();
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            if (string.IsNullOrWhiteSpace(ids))
            {
                resMsg.IsSuccess = false;
                resMsg.Message = $"传入的信息不对";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (ids.EndsWith(","))
                ids = ids.Substring(0, ids.Length - 1);
            StringBuilder strb = new StringBuilder();
            string[] id_arr = Regex.Split(ids, ",");
            foreach (var item in id_arr)
            {
                long num_id = ConvertHelper.TryPraseInt(item, 0);
                if (num_id > 0)
                {
                    var obj = dbcontext.ADZ_BusinessMsg_SysUser.Where(t => (id_arr.Contains(t.Id.ToString()) && t.IsDel == 0 && t.UID == userinfo.user.uid));
                    obj.ToList().ForEach(t => t.IsDel = 1);
                    dbcontext.SaveChanges();
                }
            }
            if (strb.Length == 0)
            {
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            else
            {
                resMsg.IsSuccess = false;
                resMsg.Message = strb.ToString();
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        [Description("设置已读")]
        [NoVerify]
        public ActionResult readmsg(string ids)
        {
            var userinfo = new CurrentUser();
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            if (string.IsNullOrWhiteSpace(ids))
            {
                resMsg.IsSuccess = false;
                resMsg.Message = $"传入的信息不对";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (ids.EndsWith(","))
                ids = ids.Substring(0, ids.Length - 1);
            StringBuilder strb = new StringBuilder();
            string[] id_arr = Regex.Split(ids, ",");
            foreach (var item in id_arr)
            {
                long num_id = ConvertHelper.TryPraseInt(item, 0);
                if (num_id > 0)
                {
                    var obj = dbcontext.ADZ_BusinessMsg_SysUser.Where(t => (id_arr.Contains(t.Id.ToString()) && t.IsRead == 0 && t.IsDel == 0 && t.UID == userinfo.user.uid));
                    obj.ToList().ForEach(t =>
                    {
                        t.IsRead = 1;
                        t.ReadTime = DateTime.Now;
                    });
                    dbcontext.SaveChanges();
                }
            }
            if (strb.Length == 0)
            {
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            else
            {
                resMsg.IsSuccess = false;
                resMsg.Message = strb.ToString();
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        [Description("设置全部已读")]
        [NoVerify]
        public ActionResult readallmsg()
        {
            ResponseMsg resMsg = new ResponseMsg();
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            StringBuilder strb = new StringBuilder();
            var userinfo = new CurrentUser();

            var obj = dbcontext.ADZ_BusinessMsg_SysUser.Where(t => t.UID == userinfo.user.uid && t.IsRead == 0 && t.IsDel == 0);
            obj.ToList().ForEach(t =>
            {
                t.IsRead = 1;
                t.ReadTime = DateTime.Now;
            });
            dbcontext.SaveChanges();

            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }





        #region 获取列表
        [Description("业务消息列表")]
        [NoVerify]
        public ActionResult MsgList(BusinessMsgQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var userinfo = new CurrentUser();

            var queryable = dbcontext.ADZ_BusinessMsg_SysUser.Where(t => t.UID == userinfo.user.uid && t.IsDel == 0);
            if (queryModel.isread != -10)
            {
                queryable = queryable.Where(t => t.IsRead == queryModel.isread);
            }

            int rowcounts = queryable.Count();
            int notreadcounts = queryable.Where(t => t.IsRead == 0).Count();
            var querylist = queryable.OrderByDescending(t => t.CreateTime)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            List<UserBusinessMsg> resultList = new List<UserBusinessMsg>();
            querylist.ForEach(t =>
            {
                var msg = new UserBusinessMsg();
                msg.aDZ_BusinessMsg_SysUser = t;
                msg.aDZ_BusinessMsg = dbcontext.ADZ_BusinessMsg.FirstOrDefault(x => x.seq_id == t.BusinessMsgId);
                resultList.Add(msg);
            });


            ViewData["AllCount"] = rowcounts;
            ViewData["NotReadCount"] = rowcounts;

            PagedList<UserBusinessMsg> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<UserBusinessMsg>(resultList, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }


        #endregion



        #region 获取未读消息数量
        [Description("获取未读消息数量")]
        [NoVerify]
        public ActionResult MsgCount()
        {
            ResponseMsg resMsg = new ResponseMsg();

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var userinfo = new CurrentUser();

            var queryable = dbcontext.ADZ_BusinessMsg_SysUser.Where(t => t.UID == userinfo.user.uid && t.IsDel == 0 && t.IsRead == 0);

            int rowcounts = queryable.Count();
            int newCounts = queryable.Count(t => t.IsNew == 0);

            if (newCounts > 0)
            {
                queryable.Where(t => t.IsNew == 0).ToList().ForEach(t =>
                {
                    t.IsNew = 1;
                });
                dbcontext.SaveChanges();
            }

            resMsg.Body = new
            {
                AllCount = rowcounts,
                NewCounts = newCounts
            };

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }


        #endregion
    }
}